// 操作人ID
var loginData = JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user;


//页大小
var pageSize = 20;

$(function () {
    //绑定所属渠道下拉列表
    allChannelConfig();
})

//绑定所属渠道下拉列表
function allChannelConfig () {
    $.ajax({
        type: "post",
        url: root.dockingplatform.allChannelConfig,
        cache: false,
        dataType: "json",
        success: function (data) {
            if (data.errorCode == "0") {
                var content = "";
                data.data.forEach(function (value, index, array) {
                    content += "<option value='" + value.customerId + "'>" + value.customerName + "</option>";
                });
                $("#parentCustomerId").append(content);
            }
        },
        error: function () {
            $.Huimodalalert('获取所属渠道信息失败', 2000);
        }
    });
}

//渠道消息管理-列表
function queryMessageList () {
    var customerId = $("#parentCustomerId").val();
    if (customerId == "0") {
        $.Huimodalalert('请选择渠道商', 2000);
        return false;
    }
    var json = {
        curPage: 1,
        pageSize: pageSize,
        parentCustomerId: customerId,//渠道商
        eventType: $("#eventType").val(),//消息类型
        eventData: $("#eventData").val(),//关键词
        eventStatus: $("#eventStatus").val()//读取状态
    }
    $.ajax({
        url: root.dockingplatform.queryMessageList,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(json),
        success: function (result) {
            $("input[name='pageNum']").val(result.data.pageNumber);
            initDataTable_Shield(result);
            $('.M-box3').pagination({
                pageCount: result.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    json.curPage = api.getCurrent();
                    $.ajax({
                        url: root.dockingplatform.queryMessageList,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType: 'json',
                        contentType: "application/json",
                        data: JSON.stringify(json),
                        success: function (result) {
                            $("input[name='pageNum']").val(result.data.pageNumber);
                            initDataTable_Shield(result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！', 2000);
        }
    });
}

//渠道消息管理-渲染列表+分页
function initDataTable_Shield (dataSource) {
    var dttable = $('#shanDataTable').dataTable(); // 初始化dataTable，清空上一个值
    dttable.fnClearTable(); //清空一下table
    dttable.fnDestroy(); //还原初始化了的datatable
    $("#shanDataTable").DataTable({
        "order": [[1, "desc"]],
        "autoWidth": false,
        "ordering": false,
        "searching": false,
        "paging": false,
        "retrieve": true,
        "destroy": true,
        "bAutoWidth": true,
        "bLengthChange": true,
        "aLengthMenu": [5, 10, 25, 50],
        "oLanguage": {
            "sLengthMenu": "每页显示 _MENU_ 条记录",
            "sZeroRecords": "抱歉， 没有找到",
            "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
            "sInfoEmpty": "",
            "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "前一页",
                "sNext": "后一页",
                "sLast": "尾页"
            },
            "sZeroRecords": "没有检索到数据",
            "sProcessing": "<img src='./loading.gif' />",
        },
        data: dataSource.data.result,
        columnDefs: [
            {
                "render": function (data, type, row, meta) {
                    var val = row.id
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 0
            },
            {
                "render": function (data, type, row, meta) {
                    var val = row.eventTypeName
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 1
            },
            {
                "render": function (data, type, row) {
                    var val = row.eventData
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 2
            },
            {
                "render": function (data, type, row) {
                    var val = row.createdAtStr
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 3
            },
            {
                "render": function (data, type, row) {
                    var val = row.eventStatusName
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 4
            },
            {
                "render": function (data, type, row) {
                    var val = row.updatedAtStr
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 5
            },
            {
                "render": function (data, type, row) {
                    var val = row.eventMessage
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 6
            },
            {
                "render": function (data, type, row) {
                    var val = row.eventTimes
                    if (val == undefined) {
                        val = ''
                    }
                    return val;
                },
                "targets": 7
            },
            {
                "render": function (data, type, row) {
                    var val = "<a href='javascript:0;' onclick=\"messageRePush(" + row.id + ",'" + row.parentCustomerId + "','" + row.eventType + "')\">重新推送</a> ";
                    return val;
                },
                "targets": 8
            },
        ]
    });
};

//渠道消息管理-推送推送
function messageRePush (id,parentCustomerId,eventType) {
    var param = { 
                    id: id,                             //主键
                    parentCustomerId: parentCustomerId, //渠道编码id
                    eventType: eventType                //消息类型:2=商品价格变更;4=商品上下架变更消息;5=订单已妥投/拒收;6=添加、删除商品池内商品：10=订单取消;16=商品介绍及规格参数变更消息
                };
    $.ajax({
        url: root.dockingplatform.messageRePush,
        cache: false,  //禁用缓存
        type: "POST",
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(param),
        success: function (result) {
            $.Huimodalalert(result.msg, 2000);
            if (result.errorCode == 0) {
                queryMessageList();
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
    });
}